package com.tianqu.tms.www.statistics.service;

import java.math.BigDecimal;
import java.text.ParseException;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.List;

import com.tianqu.tms.core.exception.TmsDaoException;
import com.tianqu.tms.core.util.DateUtil;
import com.tianqu.tms.www.statistics.dao.TmsMarketCarAgeCountDao;
import com.tianqu.tms.www.statistics.dao.TmsMarketCarPriceDayCountDao;
import com.tianqu.tms.www.statistics.dao.TmsMarketCarStyleCountDao;
import com.tianqu.tms.www.statistics.dao.TmsMarketCarVendorDayCountDao;
import com.tianqu.tms.www.statistics.dao.TmsMarketRegisterDayCountDao;
import com.tianqu.tms.www.statistics.dao.TmsMarketRegisterMonthCountDao;
import com.tianqu.tms.www.statistics.dao.TmsSellerRegisterDayCountDao;
import com.tianqu.tms.www.statistics.dto.ComboDto;
import com.tianqu.tms.www.statistics.dto.InventoryVehicleDto;
import com.tianqu.tms.www.statistics.entity.TmsMarketRegisterDayCountDto;
import com.tianqu.tms.www.statistics.entity.TmsMarketRegisterMonthCountDto;

public class InventoryVehicleService {

	/**
	 * 累计统计
	 * @return
	 * @throws TmsDaoException
	 */
	public InventoryVehicleDto getCumulativeStatisticalInfo() throws TmsDaoException{

		InventoryVehicleDto inventoryVehicleDto = new InventoryVehicleDto();
		
		// 库存车辆日登记数量统计表
		TmsMarketRegisterDayCountDao tmsMarketRegisterDayCountDao = new TmsMarketRegisterDayCountDao();
		TmsMarketRegisterDayCountDto dto1 = tmsMarketRegisterDayCountDao.getStartCountDate();
		TmsMarketRegisterDayCountDto dto2 = tmsMarketRegisterDayCountDao.getCumulativeStatisticalInfo();

		try {
			
			Date systemDay = new Date();
			Date date = DateUtil.string102ToDate(dto1.getCountDate());
			int days = (int) ((systemDay.getTime() - date.getTime()) / (1000*3600*24));

			// 登记总数
			inventoryVehicleDto.setRegNum(dto2.getRegNum().divide(new BigDecimal(10000), 2 ,BigDecimal.ROUND_HALF_UP));
			
			// 登记总价
			inventoryVehicleDto.setPrice(dto2.getPrice().divide(new BigDecimal(100000000), 2 ,BigDecimal.ROUND_HALF_UP));
			
			// 统计日期
			inventoryVehicleDto.setCountDate(DateUtil.DateToString11(date));
			
			// 统计天数
			inventoryVehicleDto.setCountDays(days);

		} catch (ParseException e) {
			e.printStackTrace();
		}

		return inventoryVehicleDto;
	}

	/**
	 * 实时统计
	 * @return
	 * @throws TmsDaoException
	 */
	public InventoryVehicleDto getRealTimeStatisticalInfo() throws TmsDaoException{

		InventoryVehicleDto inventoryVehicleDto = new InventoryVehicleDto();

		java.text.DecimalFormat myformat = new java.text.DecimalFormat("0.0");

		// 库存车辆日登记数量统计表
		TmsMarketRegisterDayCountDao tmsMarketRegisterDayCountDao = new TmsMarketRegisterDayCountDao();
		TmsMarketRegisterDayCountDto dto1 = tmsMarketRegisterDayCountDao.getRealTimeStatisticalInfo();

		// 在库数量
		inventoryVehicleDto.setOnlineNum(dto1.getOnlineNum());
		
		// 空位数
		inventoryVehicleDto.setFreeNum(dto1.getFreeNum());
		
		// 车位使用率
		inventoryVehicleDto.setRatio(myformat.format(dto1.getRatio().multiply(new BigDecimal(100))));

		return inventoryVehicleDto;
	}

	/**
	 * 今天/当月新增库存数量、当月新增同比、当月新增环比
	 * 今天/当月新增库存总值、当月新增同比、当月新增环比
	 * 本月平均车位使用率、本月平均车位使用率同比、本月平均车位使用率环比
	 * @return
	 * @throws TmsDaoException
	 */
	public InventoryVehicleDto getNewInventoryNumber() throws TmsDaoException{

		InventoryVehicleDto inventoryVehicleDto = new InventoryVehicleDto();

		Calendar cal = Calendar.getInstance();
		int year = cal.get(Calendar.YEAR);
		int month = cal.get(Calendar.MONTH ) + 1;
		int day = cal.get(Calendar.DAY_OF_MONTH);
		
		// 上一年月份总天数
		int yesteryearMonthDays = DateUtil.getDaysByYearMonth(year - 1, month);

		java.text.DecimalFormat myformat = new java.text.DecimalFormat("0.0");

		// 库存车辆日登记数量统计表
		TmsMarketRegisterDayCountDao tmsMarketRegisterDayCountDao = new TmsMarketRegisterDayCountDao();
		TmsMarketRegisterDayCountDto dto1 = tmsMarketRegisterDayCountDao.getTodayNewInventory();

		// 库存车辆登记数量月统计表
		TmsMarketRegisterDayCountDto dto2 = tmsMarketRegisterDayCountDao.getMonthNewInventory();

		// 上月平均车位使用率/上一年度当前月份入库总量/上一年度当前月份入库总价
		TmsMarketRegisterDayCountDto dto3 = tmsMarketRegisterDayCountDao.getLastMonthParkingRate();
		
		// 上一年度当前月份入库总量/上一年度同期累计新增总价/上一年度同期月平均车位使用率
		TmsMarketRegisterDayCountDto dto4 = tmsMarketRegisterDayCountDao.getLastYearTimeParkingRate();
				
		// 今天新增库存数量
		inventoryVehicleDto.setTodayNewInventoryNum(dto1.getRegNum());

		// 当月新增库存数量
		inventoryVehicleDto.setMonthNewInventoryNum(dto2.getRegNum());

		BigDecimal monthNewInventoryNumSequential = BigDecimal.ZERO;

		if(BigDecimal.ZERO.compareTo(dto3.getRegNum()) != 0) {
			
			monthNewInventoryNumSequential = dto2.getRegNum().subtract(dto3.getRegNum()).divide(dto3.getRegNum(),4,BigDecimal.ROUND_HALF_DOWN).multiply(new BigDecimal(100));
		}

		// 当月新增库存数量环比是否是增长
		if(BigDecimal.ZERO.compareTo(monthNewInventoryNumSequential) <= 0) {
			
			inventoryVehicleDto.setMonthNewInventoryNumSequentialFlg(true);
		}else {
			monthNewInventoryNumSequential = new BigDecimal(-1).multiply(monthNewInventoryNumSequential);
			inventoryVehicleDto.setMonthNewInventoryNumSequentialFlg(false);
		}

		// 当月新增库存数量环比
		inventoryVehicleDto.setMonthNewInventoryNumSequential(myformat.format(monthNewInventoryNumSequential) + "%");

		// 当月新增库存数量同比
		BigDecimal monthNewInventoryNum = dto4.getRegNum().multiply(new BigDecimal(day)).divide(new BigDecimal(yesteryearMonthDays), 4, BigDecimal.ROUND_HALF_DOWN);

		BigDecimal sameMonthNewInventoryNum = BigDecimal.ZERO;

		if(BigDecimal.ZERO.compareTo(monthNewInventoryNum) != 0) {
			
			sameMonthNewInventoryNum = dto2.getRegNum().subtract(dto4.getRegNum()).divide(monthNewInventoryNum, 4, BigDecimal.ROUND_HALF_DOWN).multiply(new BigDecimal(100));
		}

		// 当月新增库存数量同比是否是增长
		if(BigDecimal.ZERO.compareTo(sameMonthNewInventoryNum) <= 0) {
			
			inventoryVehicleDto.setMonthNewInventoryNumSameCompareFlg(true);
		}else {
			sameMonthNewInventoryNum = new BigDecimal(-1).multiply(sameMonthNewInventoryNum);
			inventoryVehicleDto.setMonthNewInventoryNumSameCompareFlg(false);
		}

		inventoryVehicleDto.setMonthNewInventoryNumSameCompare(myformat.format(sameMonthNewInventoryNum) + "%");;

		// 今天新增库存总值
		inventoryVehicleDto.setTodayNewInventoryPrice(dto1.getPrice().divide(new BigDecimal(10000)));

		// 当月新增库存总值
		inventoryVehicleDto.setMonthNewInventoryPrice(dto2.getPrice().divide(new BigDecimal(10000)));

		BigDecimal monthNewInventoryPriceSequential = BigDecimal.ZERO;

		if(BigDecimal.ZERO.compareTo(dto3.getPrice()) != 0) {
			
			monthNewInventoryPriceSequential = dto2.getPrice().subtract(dto3.getPrice()).divide(dto3.getPrice(),4,BigDecimal.ROUND_HALF_DOWN).multiply(new BigDecimal(100));
		}

		// 当月新增库存总值环比是否是增长
		if(BigDecimal.ZERO.compareTo(monthNewInventoryPriceSequential) <= 0) {
			
			inventoryVehicleDto.setMonthNewInventoryPriceSequentialFlg(true);
		}else {
			monthNewInventoryPriceSequential = new BigDecimal(-1).multiply(monthNewInventoryPriceSequential);
			inventoryVehicleDto.setMonthNewInventoryPriceSequentialFlg(false);
		}

		// 当月新增库存总值环比
		inventoryVehicleDto.setMonthNewInventoryPriceSequential(myformat.format(monthNewInventoryPriceSequential) + "%");

		// 当月新增库存总值同比
		BigDecimal monthNewInventoryPrice = dto4.getPrice().multiply(new BigDecimal(day)).divide(new BigDecimal(yesteryearMonthDays), 4, BigDecimal.ROUND_HALF_DOWN);

		BigDecimal sameMonthNewInventoryPrice = BigDecimal.ZERO;

		if(BigDecimal.ZERO.compareTo(monthNewInventoryPrice) != 0) {
	
			sameMonthNewInventoryPrice = dto2.getPrice().subtract(dto4.getPrice()).divide(monthNewInventoryPrice, 4, BigDecimal.ROUND_HALF_DOWN).multiply(new BigDecimal(100));
		}

		// 当月新增库存总值同比是否是增长
		if(BigDecimal.ZERO.compareTo(sameMonthNewInventoryPrice) <= 0) {
			
			inventoryVehicleDto.setMonthNewInventoryPriceSameCompareFlg(true);
		}else {
			sameMonthNewInventoryPrice = new BigDecimal(-1).multiply(sameMonthNewInventoryPrice);
			inventoryVehicleDto.setMonthNewInventoryPriceSameCompareFlg(false);
		}

		inventoryVehicleDto.setMonthNewInventoryPriceSameCompare(myformat.format(sameMonthNewInventoryPrice) + "%");
		
		// 本月平均车位使用率
		inventoryVehicleDto.setMonthParkingSpaceAveRatio(myformat.format(dto2.getRatio().multiply(new BigDecimal(100))));

		BigDecimal monthParkingSpaceAveRatioSequential = BigDecimal.ZERO;

		if(BigDecimal.ZERO.compareTo(dto3.getRatio()) != 0) {
	
			monthParkingSpaceAveRatioSequential = dto2.getRatio().divide(dto3.getRatio(), 4, BigDecimal.ROUND_HALF_DOWN);
		}

		// 本月平均车位使用率环比是否是增长
		if(BigDecimal.ZERO.compareTo(monthParkingSpaceAveRatioSequential) <= 0) {
			
			inventoryVehicleDto.setMonthParkingSpaceAveRatioSequentialFlg(true);
		}else {
			monthParkingSpaceAveRatioSequential = new BigDecimal(-1).multiply(monthParkingSpaceAveRatioSequential);
			inventoryVehicleDto.setMonthParkingSpaceAveRatioSequentialFlg(false);
		}

		// 本月平均车位使用率环比
		inventoryVehicleDto.setMonthParkingSpaceAveRatioSequential(myformat.format(monthParkingSpaceAveRatioSequential) + "%");

		BigDecimal monthParkingSpaceAveRatioSameCompare = BigDecimal.ZERO;

		if(BigDecimal.ZERO.compareTo(dto4.getRatio()) != 0) {
			
			monthParkingSpaceAveRatioSameCompare = dto2.getRatio().divide(dto4.getRatio(), 4, BigDecimal.ROUND_HALF_DOWN);
		}

		// 本月平均车位使用率同比是否是增长
		if(BigDecimal.ZERO.compareTo(monthParkingSpaceAveRatioSameCompare) <= 0) {
			
			inventoryVehicleDto.setMonthParkingSpaceAveRatioSameCompareFlg(true);
		}else {
			monthParkingSpaceAveRatioSameCompare = new BigDecimal(-1).multiply(monthParkingSpaceAveRatioSameCompare);
			inventoryVehicleDto.setMonthParkingSpaceAveRatioSameCompareFlg(false);
		}

		// 本月平均车位使用率同比
		inventoryVehicleDto.setMonthParkingSpaceAveRatioSameCompare(myformat.format(monthParkingSpaceAveRatioSameCompare) + "%");

		return inventoryVehicleDto;
	}

	/**
	 * 品牌TOP5
	 * @return
	 * @throws TmsDaoException
	 */
	public InventoryVehicleDto getBrandTop5() throws TmsDaoException{

		InventoryVehicleDto inventoryVehicleDto = new InventoryVehicleDto();

		// 品牌TOP5
		TmsMarketCarVendorDayCountDao tmsMarketCarVendorDayCountDao = new TmsMarketCarVendorDayCountDao();
		List<ComboDto> tmsMarketCarVendorDayCountList = tmsMarketCarVendorDayCountDao.getBrandTop5();
		
		inventoryVehicleDto.setBrandTop5(tmsMarketCarVendorDayCountList);
		
		return inventoryVehicleDto;
	}

	/**
	 * 车系TOP5
	 * @return
	 * @throws TmsDaoException
	 */
	public InventoryVehicleDto getCarStyleTop5() throws TmsDaoException{

		InventoryVehicleDto inventoryVehicleDto = new InventoryVehicleDto();

		// 车系TOP5
		TmsMarketCarStyleCountDao tmsMarketCarStyleCountDao = new TmsMarketCarStyleCountDao();
		List<ComboDto> tmsMarketCarStyleCountList = tmsMarketCarStyleCountDao.getCarStyleTop5();
		
		inventoryVehicleDto.setCarStyleTop5(tmsMarketCarStyleCountList);
		
		return inventoryVehicleDto;
	}

	/**
	 * 库存平均车价商户TOP5
	 * @return
	 * @throws TmsDaoException
	 */
	public InventoryVehicleDto getAverageCarPricesForMerchantsTop5() throws TmsDaoException{

		InventoryVehicleDto inventoryVehicleDto = new InventoryVehicleDto();

		// 库存平均车价商户TOP5
		TmsSellerRegisterDayCountDao tmsSellerRegisterDayCountDao = new TmsSellerRegisterDayCountDao();
		List<ComboDto> tmsSellerRegisterDayCountList = tmsSellerRegisterDayCountDao.getAverageCarPricesForMerchantsTop5();
		
		inventoryVehicleDto.setAverageCarPricesForMerchantsTop5(tmsSellerRegisterDayCountList);
		
		return inventoryVehicleDto;
	}

	/**
	 * 库存量商户TOP5
	 * @return
	 * @throws TmsDaoException
	 */
	public InventoryVehicleDto getInventoryTop5() throws TmsDaoException{

		InventoryVehicleDto inventoryVehicleDto = new InventoryVehicleDto();

		// 库库存量商户TOP5
		TmsSellerRegisterDayCountDao tmsSellerRegisterDayCountDao = new TmsSellerRegisterDayCountDao();
		List<ComboDto> tmsSellerRegisterDayCountList = tmsSellerRegisterDayCountDao.getInventoryTop5();
		
		inventoryVehicleDto.setInventoryTop5(tmsSellerRegisterDayCountList);
		
		return inventoryVehicleDto;
	}

	/**
	 * 库存车辆价格分布
	 * @return
	 * @throws TmsDaoException
	 */
	public InventoryVehicleDto getInventoryCarsPrice() throws TmsDaoException{

		InventoryVehicleDto inventoryVehicleDto = new InventoryVehicleDto();

		// 库存车辆价格分布
		TmsMarketCarPriceDayCountDao tmsMarketCarPriceDayCountDao = new TmsMarketCarPriceDayCountDao();
		List<ComboDto> tmsMarketCarPriceDayCountList = tmsMarketCarPriceDayCountDao.getInventoryCarsPrice();
		
		inventoryVehicleDto.setInventoryCarsPrice(tmsMarketCarPriceDayCountList);
		
		return inventoryVehicleDto;
	}

	/**
	 * 库存车龄分布
	 * @return
	 * @throws TmsDaoException
	 */
	public InventoryVehicleDto getInventoryCarsAge() throws TmsDaoException{

		InventoryVehicleDto inventoryVehicleDto = new InventoryVehicleDto();

		// 库存车龄分布
		TmsMarketCarAgeCountDao tmsMarketCarAgeCountDao = new TmsMarketCarAgeCountDao();
		List<ComboDto> tmsMarketCarAgeCountList = tmsMarketCarAgeCountDao.getInventoryCarsAge();
		
		inventoryVehicleDto.setInventoryCarsAge(tmsMarketCarAgeCountList);
		
		return inventoryVehicleDto;
	}

	/**
	 * 最近10个月内新增入库走势图/最近10个月内车位使用率走势图
	 * @return
	 * @throws TmsDaoException
	 */
	public InventoryVehicleDto get10MonthNewStorageAndParkingRate() throws TmsDaoException{

		InventoryVehicleDto inventoryVehicleDto = new InventoryVehicleDto();

		java.text.DecimalFormat myformat = new java.text.DecimalFormat("0.0");

		// 最近0-9个月内新增入库/最近0-9个月内车位使用率
		TmsMarketRegisterMonthCountDao tmsMarketRegisterMonthCountDao = new TmsMarketRegisterMonthCountDao();
		TmsMarketRegisterMonthCountDto tmsMarketRegisterMonthCountDto = tmsMarketRegisterMonthCountDao.get09MonthNewStorageAndParkingRate();
		
		List<ComboDto> monthNewStorageList = tmsMarketRegisterMonthCountDto.getMonthNewStorageList();
		List<ComboDto> monthParkingRateList = tmsMarketRegisterMonthCountDto.getMonthParkingRateList();

		Collections.reverse(monthNewStorageList);
		Collections.reverse(monthParkingRateList);
	
//		for(ComboDto dto : monthParkingRateList) {
//			dto.setValue(dto.getValue() + "%");
//		}
		
		// 第10个月内新增入库/第10个月内车位使用率
		TmsMarketRegisterDayCountDao tmsMarketRegisterDayCountDao = new TmsMarketRegisterDayCountDao();
		TmsMarketRegisterDayCountDto tmsMarketRegisterDayCountDto = tmsMarketRegisterDayCountDao.getD10MonthNewStorageAndParkingRate();

		ComboDto dto1 = new ComboDto();
		dto1.setName(tmsMarketRegisterDayCountDto.getResDate());
		dto1.setValue(String.valueOf(tmsMarketRegisterDayCountDto.getRegNum()));

		ComboDto dto2 = new ComboDto();
		dto2.setName(tmsMarketRegisterDayCountDto.getResDate());
		dto2.setValue(String.valueOf(new BigDecimal(100).multiply(tmsMarketRegisterDayCountDto.getRatio())));

		monthNewStorageList.add(dto1);
		monthParkingRateList.add(dto2);

		// 最近10个月内新增入库
		inventoryVehicleDto.setMonthNewStorage(monthNewStorageList);
		// 最近10个月内车位使用率
		inventoryVehicleDto.setMonthParkingRate(monthParkingRateList);

		return inventoryVehicleDto;
	}
}
